//
//  StringExample.swift
//  SwiftTest
//
//  Created by bym on 2019/8/8.
//  Copyright © 2019 ZZ. All rights reserved.
//

import UIKit

class StringExample: NSObject {
	
	var str : String = ""
	
	override init() {
		super.init()
		frequencyMethodProperty()
	}
	
	func initMethod() {
		str = "abc1"
		ZZLog(str)
		
		//String(<#T##characters: Sequence##Sequence#>)
		str = String("abc2")
		ZZLog(str)
		
		let charArr: [CChar] = [67, 97, 102, -61, -87, 0]
		charArr.withUnsafeBufferPointer { ptr in
		let str = String(cString: ptr.baseAddress!)
			ZZLog(str)
		}
		// Prints "Café"
		
		//init(_ c: Character)
		let str = String.init("😂")
		ZZLog(str)
		
		//init(_ substring: __shared Substring)
	}
	
	func addInsert() {
		//append
		str = "😂😆"
		str.append("🙃")
		ZZLog(str)
		str.append(contentsOf: "表情包")
		ZZLog(str)
		//+
		str = "你是" + str
		ZZLog(str)
		
		
		//insert
		str.insert(contentsOf: "哈哈", at: str.startIndex)
		ZZLog(str)
		
		let range = str.range(of: "哈哈")
		str.insert(contentsOf: ",", at: range!.upperBound)
		ZZLog(str)
		str.insert(contentsOf: "began:", at: range!.lowerBound)
		ZZLog(str)
		str.insert(contentsOf: ".", at: str.endIndex)
		ZZLog(str)
	}
	
	func remove() {
		str = "😂😆🙃😋👌😢"
		str.remove(at: str.index(before: str.endIndex))
		ZZLog(str)
		
		str = "😂😆🙃😋👌😢"
		let range = str.range(of: "😋👌")
		str.removeSubrange(range!)
		ZZLog(str)
	}
	
	//replace
	func replace() {
		str = "😂😆🙃😋👌😢"
		let range = str.range(of: "🙃😋👌😢")
		str.replaceSubrange(range!, with: "😂😆")
		ZZLog(str)
	}
	
	//subStr
	func subStr() {
		str = "😂😆🙃😋👌😢"
		let nsString = str as NSString
		
		let nsRange1 = nsString.range(of: "😆🙃")
		let nsRange2 = nsString.range(of: "👌")
		let resRange = NSRange(location: nsRange1.location, length: nsRange2.location + nsRange2.length - nsRange1.location)
		let range = Range(resRange, in: str)!
		ZZLog(str.substring(with: range))
	}
	
	func compare() {
		let str = "😂😆🙃😋"
		let nsStr = "😂😆🙃😋" as NSString
		let tempStr = nsStr as String
		ZZLog(str == tempStr)
		
	}
	
	func frequencyMethodProperty() {
		let str = "😂😆🙃😋"
		ZZLog(str.hasPrefix("😂"))
		ZZLog(str.hasSuffix("🙃😋"))
		
		ZZLog(str.prefix(1))
		ZZLog(str.suffix(2))
		
		ZZLog(str.isEmpty)
		ZZLog(str.count)
		ZZLog(str.first)
		ZZLog(str.last)
		//startIndex : String.Index
		ZZLog(str.startIndex)
		//endIndex
		ZZLog(str.endIndex)
	}
}
